﻿using System;
using System.Text;
using Skynet = Crypt.Skynet;
using cc = Crypt.Common;

namespace Common.Utils
{
    public static class GameTools
    {
        public static byte[] GenerateClientKey()
        {
            return Skynet.Crypt.RandomKey();
        }

        public static byte[] DHSecret(byte[] inX, byte[] inY)
        {
            return Skynet.Crypt.DHSecret(inX, inY);
        }

        public static byte[] DHExchange(byte[] inBytes)
        {
            return Skynet.Crypt.DHExchange(inBytes);
        }

        public static byte[] HMac64(byte[] inX, byte[] inY)
        {
            return Skynet.Crypt.HMac64(inX, inY);
        }

        public static string Base64EncodeFromBytes(byte[] inBytes)
        {
            return Skynet.Base64.EncodeFromBytes(inBytes);
        }

        public static byte[] Base64DecodeToBytes(string inStr)
        {
            return Skynet.Base64.DecodeToBytes(inStr);
        }

        public static string Base64Encode(string inStr)
        {
            return Skynet.Base64.Encode(inStr);
        }

        public static string Base64Decode(string inStr)
        {
            return Skynet.Base64.Decode(inStr);
        }

        public static byte[] DesEncrypt(string inStr, string key)
        {
            byte[] byteKey = Encoding.UTF8.GetBytes(key);
            return DesEncrypt(inStr, byteKey);
        }

        public static byte[] DesEncrypt(string inStr, byte[] key)
        {
            byte[] byteEncrypt = Encoding.UTF8.GetBytes(inStr);
            return Skynet.Des.Encrypt(byteEncrypt, key);
        }

        public static string DesDecrypt(byte[] inStr, string key)
        {
            byte[] byteKey = Encoding.UTF8.GetBytes(key);
            return DesDecrypt(inStr, byteKey);
        }

        public static string DesDecrypt(byte[] inStr, byte[] key)
        {
            return Encoding.UTF8.GetString(Skynet.Des.Decrypt(inStr, key));
        }

        /// <summary>
        /// 根据GUID获取16位的唯一字符串
        /// </summary>
        /// <returns></returns>
        public static string GenerateStringGUID()
        {
            long i = 1;
            foreach (byte b in Guid.NewGuid().ToByteArray())
                i *= ((int)b + 1);
            return string.Format("{0:x}", i - DateTime.Now.Ticks);
        }

        /// <summary>
        /// 根据GUID获取19位的唯一数字序列
        /// </summary>
        /// <returns></returns>
        public static long GenerateLongGUID()
        {
            byte[] buffer = Guid.NewGuid().ToByteArray();
            return BitConverter.ToInt64(buffer, 0);
        }

        /// <summary>
        /// C#中随机生成指定长度的密码
        /// </summary>
        public static string GeneratePassword(int pwdLength = 6)
        {
            //声明要返回的字符串
            string tmpstr = "";
            //密码中包含的字符数组
            string pwdchars = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            //数组索引随机数
            int iRandNum;
            //随机数生成器
            Random rnd = new Random();
            for (int i = 0; i < pwdLength; i++)
            {
                //Random类的Next方法生成一个指定范围的随机数
                iRandNum = rnd.Next(pwdchars.Length);
                //tmpstr随机添加一个字符
                tmpstr += pwdchars[iRandNum];
            }
            return tmpstr;
        }
    }

}


